
from abc import abstractmethod
import urllib.parse
import httplib2
import copy
import json
import re
import types



class DownloadVideoFromYouTuBe():
    def __init__(self):
        print("------------start-------")
    def getCheckBody(self):
        return {
            'click_url':  'http://www.clipconverter.cc/check.php',
             'mediaurl': 'https://www.youtube.com/watch?v=ABp938_C1E0',
            'filename':  None,
            'filetype':  None,
            'format':  None,
            'audiovol':  0,
            'audiochannel':  2,
            'audiobr':  128,
            'videobr':  224,
            'videores':  "352x288",
            'videoaspect':  None,
            'customres':  "320x240",
            'auto':  0,
            'hash':  None,
            'image':  None,
            'videoid':  None,
            'pattern':  None,
            'server':  None,
            'serverinterface':  None,
            'service':  None,
            'ref':  None,
            'lang':  "en",
            'client_urlmap':  "none",
            'ipv6':  False,
            'addon_urlmap':  None,
            'cookie' :  None,
            'addon_cookie' :  None,
            'addon_title' :  None,
            'ablock':  0,
            'clientside' :  0,
            'addon_page' :  "none",
            'verify' :  None,
            'result' :  None,
            'again' :  None,
            'addon_browser' :  None,
            'addon_version' :  None,
            'timefrom-start': '1', 'timeto-end': '1', 'id3-artist': 'None', 'id3-title': 'None',
            'id3-album': 'ClipConverter.cc', 'org-filename': 'None'
        }
    def getConvertBody(self,content):
        json_obj = json.loads(content)
        print(json_obj)
        urls = json_obj['url']
        body = {
               'click_url' : 'http://www.clipconverter.cc/check.php',
               'mediaurl' : 'https://www.youtube.com/watch?v=ABp938_C1E0',
               'audiovol' : "0",
               'audiochannel' : "2",
               'audiobr' : "128",
               'videobr' : "224",
               'videores' : "352x288",
               'videoaspect' : None,
               'customres' : "320x240",
               'auto' : 1,
               'hash' : None,
               'ref' : None,
               'lang' : "en",
               'client_urlmap' : None,
               'ipv6' : False,
               'addon_urlmap' : None,
               'cookie' : None,
               'addon_cookie' : None,
               'addon_title' : None,
               'ablock' : 0,
                'addon_page':None,
                'result ': None,
                'again': None,
                'addon_browser': None,
                'addon_version': None,
                'timefrom - start': 1,
                'timeto - end': 1,
                'id3 - artist': json_obj['id3title'],
                'id3 - title': json_obj['id3artist'],
                'id3 - album': "ClipConverter.cc",
                'org - filename': "柳公權楷書─玄秘塔4",
                'url':urls[0]['url'],
                'filetype':urls[0]['filetype'],
                'format':json_obj['format'],
                'verify':json_obj['verify'],
                'videoid':json_obj['videoid'],
                'server':json_obj['server'],
                'image':json_obj['thumb'],
                'channel':json_obj['channel'],
                'serverinterface':json_obj['serverinterface'],
                'clientside':json_obj['clientside'],
                'service':json_obj['service'],
                'pattern':json_obj['pattern']
        }
        return body

    def click_search(self,body):
        http = httplib2.Http()

        url = body.get("click_url")
        headers = {
                 'Content-type': 'application/x-www-form-urlencoded','X-Requested-With':'XMLHttpRequest','Accept':'application/json, text/javascript, */*'
        }
        response, content = http.request(url, 'POST', headers=headers, body=urllib.parse.urlencode(body))

        return content
    def conversion_url(self,body):
        http = httplib2.Http()
        url = body.click_url
        headers = {
            'Content-type': 'application/x-www-form-urlencoded', 'X-Requested-With': 'XMLHttpRequest',
            'Accept': 'application/json, text/javascript, */*',
            'Cookie': '__test;_gat = 1;_ga = GA1.2.934904021.1473744380'
        }
        response, content = http.request(url, 'POST', headers=headers, body=urllib.parse.urlencode(body))
        print(content)
        return content

    @staticmethod
    def _start():
        youtube_media_url = "https://www.youtube.com/watch?v=ABp938_C1E0"
        down = DownloadVideoFromYouTuBe()
        # 第一次请求获取视频id和url等信息
        body_=down.getCheckBody();
        content = down.click_search(body_)
        print(type(content))
        if isinstance(content, bytes):
            # 提取url和id 等属性
            json_str = content.decode('utf-8')
            cbody = down.getConvertBody(json_str)
        else:
            print("type error")
        # 转码
        content = down.conversion_url(cbody)






if __name__=='__main__':
    DownloadVideoFromYouTuBe._start()
